<?php

namespace App\Model;

use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class SendAllUserNotify extends BaseModel
{
    protected $table = 'send_alluser_notify'; //全员推送消息表

    protected $connection = 'api';

    public function getStartTimestampAttribute($value)
    {
        if (!$value) return 0;
        return date('Y-m-d H:i:s', $value);
    }

    public function getEndTimestampAttribute($value)
    {
        if (!$value) return 0;
        return date('Y-m-d H:i:s', $value);
    }

    //添加
    public function insertOrUpNotify($data)
    {
        return curl_request(getKkaudioApiUrl() . '/inner/officenotify/insertorupnotify', $data, 'json', 'POST');
    }
    //删除
    public function deleteNotify($notifyid,$status){
        return curl_request(getKkaudioApiUrl() . '/inner/officenotify/deletenotify', ['notifyid'=>$notifyid,'status'=>$status], 'json', 'POST');
    }
    /**
     * 全员推送列表获取
     *
     */
    public function getnotifyList($page = 1, $limit = 10,$sendstatus='',$start_time,$end_time,$notify_type)
    {
        $where = function($q)use($sendstatus,$start_time,$end_time,$notify_type) {
            if (!is_null($sendstatus)) { 
                $q->where("send_status", $sendstatus);
            }
            if(!empty($start_time) && !empty($end_time))
            {
                $q->where('create_time', '>=',$start_time);
                $end_time = Carbon::parse($end_time)->endOfDay()->toDateTimeString();
                $q->where('create_time','<=',$end_time);
            }
            if(!empty($notify_type)){
                $q->where('notify_type',$notify_type);
            }
            $q->where('is_delete',0);
        };
        $data = DB::connection($this->connection)->table($this->table)
        ->where($where)
        ->orderby('id','DESC')
        ->paginate($limit,['*'],'page',$page);
        return objToArray($data);
    }
}
